package com.ruoyi.web.controller.module.fileDownload;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.fileDownload.FileDownload;
import com.ruoyi.system.service.module.fileDownload.IFileDownloadService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 企管类相关资料格Controller
 *
 * @author ruoyi
 * @date 2024-05-10
 */
@Validated
@Api(value = "企管类相关资料格控制器", tags = {"企管类相关资料格管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/download")
public class FileDownloadController extends BaseController {

    private final IFileDownloadService fileDownloadService;

    /**
     * 查询企管类相关资料格列表
     */
    @ApiOperation("查询企管类相关资料格列表")
    @PreAuthorize("@ss.hasPermi('module:download:list')")
    @GetMapping("/list")
    public TableDataInfo<FileDownload> list(@Validated FileDownload fileDownload) {
		SysUser sysUser = getUser();
        QueryWrapper<FileDownload> qw = new QueryWrapper<>();
		if (fileDownload.getPropertyId()!=null){
			qw.eq("property_id",fileDownload.getPropertyId());
		}
		if (sysUser.getPropertyId()!=null){
			qw.eq("property_id",sysUser.getPropertyId());
		}
		if (fileDownload.getFileType()!=null){
			qw.eq("file_type",fileDownload.getFileType());
		}
		if (fileDownload.getFileName()!=null){
			qw.like("file_name",fileDownload.getFileName());
		}
        TableDataInfo<FileDownload> td = PageUtils.buildDataInfo(fileDownloadService.page(PageUtils.buildPage(),qw));
        List<FileDownload> list = td.getRows();
        return td;
    }

    /**
     * 导出企管类相关资料格列表
     */
    @ApiOperation("导出企管类相关资料格列表")
    @PreAuthorize("@ss.hasPermi('module:download:export')")
    @Log(title = "企管类相关资料格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<FileDownload> export(@Validated FileDownload fileDownload) {
        List<FileDownload> list = fileDownloadService.list();
        ExcelUtil<FileDownload> util = new ExcelUtil<FileDownload>(FileDownload.class);
        return util.exportExcel(list, "企管类相关资料格");
    }

    /**
     * 获取企管类相关资料格详细信息
     */
    @ApiOperation("获取企管类相关资料格详细信息")
    @PreAuthorize("@ss.hasPermi('module:download:query')")
    @GetMapping("/{id}")
    public AjaxResult<FileDownload> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        FileDownload fileDownload = fileDownloadService.getById(id);
        return AjaxResult.success(fileDownload);
    }

    /**
     * 新增企管类相关资料格
     */
    @ApiOperation("新增企管类相关资料格")
    @PreAuthorize("@ss.hasPermi('module:download:add')")
    @Log(title = "企管类相关资料格", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody FileDownload fileDownload) {
        return toAjax(fileDownloadService.save(fileDownload) ? 1 : 0);
    }

    /**
     * 修改企管类相关资料格
     */
    @ApiOperation("修改企管类相关资料格")
    @PreAuthorize("@ss.hasPermi('module:download:edit')")
    @Log(title = "企管类相关资料格", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody FileDownload fileDownload) {
        return toAjax(fileDownloadService.updateById(fileDownload) ? 1 : 0);
    }

    /**
     * 删除企管类相关资料格
     */
    @ApiOperation("删除企管类相关资料格")
    @PreAuthorize("@ss.hasPermi('module:download:remove')")
    @Log(title = "企管类相关资料格" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(fileDownloadService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
